package com.example.administrator.x2picturetransliteration.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.example.administrator.x2picturetransliteration.Base.BaseActivity;
import com.example.administrator.x2picturetransliteration.Base.BasePresenter;
import com.example.administrator.x2picturetransliteration.Bean.UserDataBean;
import com.example.administrator.x2picturetransliteration.Constants;
import com.example.administrator.x2picturetransliteration.Home.Presenter.Fragment.MePresenter;
import com.example.administrator.x2picturetransliteration.R;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import butterknife.BindView;
import butterknife.OnClick;

public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {

	@BindView(R.id.TitleText)TextView TitleText;
	@BindView(R.id.LButton)ImageView LButton;

	@BindView(R.id.nrText)TextView nrText;
	@BindView(R.id.ContentText)TextView ContentText;
	@BindView(R.id.img)ImageView img;


    private IWXAPI api;

    private int result;

	private MePresenter mMePresenter;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

		TitleText.setText("");
		LButton.setImageResource(R.mipmap.home_backw);

    	api = WXAPIFactory.createWXAPI(this, Constants.getConstants().getAppId(),true);
        api.handleIntent(getIntent(), this);

		mMePresenter = new MePresenter(this,mCompositeSubscriptions);

        if (getIntent().getStringExtra("no")!=null){

			nrText.setText("购买失败");
			ContentText.setText("成为会员失败了，请重新尝试哦~");
			img.setImageResource(R.drawable.home_img_gmsb);
		}


		mMePresenter.info(new BasePresenter.Callback() {
			@Override
			public void getData(Object data) {

				Constants.getConstants().setmUserDataBean(JSON.parseObject(data + "", new TypeReference<UserDataBean>() {}));

			}
		});
    }

	@Override
	public int intiLayout() {
		return R.layout.pay_result;
	}


	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);

	}

	@OnClick(R.id.left_button)
	public void left_buttonClick(){

		finish();
	}

	@OnClick(R.id.okText)
	public void okTextClick(){

		finish();
	}

	@Override
	public void onReq(BaseReq req) {

	}

	@Override
	public void onResp(BaseResp resp) {

		switch (resp.errCode) {
			case BaseResp.ErrCode.ERR_OK:
				result = 0;
				nrText.setText("购买成功!");
				break;
			case BaseResp.ErrCode.ERR_USER_CANCEL:

				result = -2;
				nrText.setText("购买失败");
				ContentText.setText("成为会员失败了，请重新尝试哦~");
				img.setImageResource(R.drawable.home_img_gmsb);
				break;
			case BaseResp.ErrCode.ERR_COMM:

				result = -1;
				nrText.setText("购买失败");
				ContentText.setText("成为会员失败了，请重新尝试哦~");
				img.setImageResource(R.drawable.home_img_gmsb);
				break;

		}

	}

	@OnClick(R.id.left_button)
	public void LButtonClick(){
		finish();
	}


}